package top.zhangjianyong.entity;

import lombok.Getter;
import lombok.Setter;
import top.zhangjianyong.utils.MD5Util;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

/**
 * 日志类型
 *
 * @author zhangjianyong
 * @since 2024/7/22 上午11:41
 */
@Getter
@Setter
public class Log {
    private Long id;
    private String remoteAddr;
    private String remoteUser;
    private String timeLocal;
    private String request;
    private String status;
    private String bodyBytesSent;
    private String httpReferer;
    private String httpUserAgent;
    private String md5;

    private final static DateTimeFormatter DTF_FROM = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss +0800", Locale.ENGLISH);
    private final static DateTimeFormatter DTF_TO = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    public Log(List<String> list, Long id) {
        StringBuffer sb = new StringBuffer();

        this.id = id;
        this.remoteAddr = list.get(1);
        sb.append("&remoteAddr=").append(remoteAddr);
        this.remoteUser = list.get(2);
        sb.append("&remoteUser=").append(remoteUser);
        String format = LocalDateTime.parse(list.get(3), DTF_FROM).format(DTF_TO);
        this.timeLocal = format;
        sb.append("&timeLocal=").append(format);
        this.request = list.get(4);
        sb.append("&request=").append(request);
        this.status = list.get(5);
        sb.append("&status=").append(status);
        this.bodyBytesSent = list.get(6);
        sb.append("&bodyBytesSent=").append(bodyBytesSent);
        this.httpReferer = list.get(7);
        sb.append("&httpReferer=").append(httpReferer);
        this.httpUserAgent = list.get(8);
        sb.append("&httpUserAgent=").append(httpUserAgent);

        this.md5 = MD5Util.md5(sb.toString());

    }


}
